package ui;

import info.Manuscript;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import users.ProgramChair;
import users.Reviewer;
import users.Role;
import users.SubprogramChair;


/**
 * This panel mimics the layout of the manuscript submission form.  It shows a different set
 * of options depending on the user role that is being used.
 * @author Aaron Chen
 * @version 3/21/2014
 */
@SuppressWarnings("serial")
public class ManuscriptViewingPanel extends ManuscriptSubmissionPanel {
  
  /** The error message to be displayed when the user attempt to submit a review
   * or recommendation past the deadline. */
  private static final String ERROR_DEADLINE = "It is passed the review deadline...";
  
  /** The panel that contains the buttons. */
  private JPanel my_panel_buttons;
  
  /** The role of the user that is viewing the manuscript. */
  private Role my_role;

  
  /**
   * Creates a new ManuscriptViewingPanel.
   * @param the_parent The main application window container.
   * @param the_manuscript The manuscript that is being viewed.
   * @param the_role The role of the user that is viewing the manuscript.
   */
  public ManuscriptViewingPanel(final DisplayPanel the_parent,
                                final Manuscript the_manuscript,
                                final Role the_role) {
    super(the_parent, the_manuscript);
    my_role = the_role;
    populatePanelWithButtons();
    lockForm(true);
  }
  
  /**
   * {@inheritDoc}
   */
  @Override
  protected JPanel createButtonsPanel() {
    my_panel_buttons = new JPanel();
    my_panel_buttons.setBackground(Standards.COLOR_BACKGROUND);
    my_panel_buttons.setBackground(Standards.COLOR_BACKGROUND);
    my_panel_buttons.setAlignmentX(Component.LEFT_ALIGNMENT);
    return my_panel_buttons;
  }
  
  /**
   * Populates the button panels with buttons.
   */
  private void populatePanelWithButtons() {
    final JButton btn_cancel = new JButton("Cancel");
    btn_cancel.addActionListener(new ActionListener() {
      /** The action to be performed.
       * Removes the form from view. */
      @Override
      public void actionPerformed(final ActionEvent the_e) {
        getDisplayStack().removeFromStack();
      }
    });
    
    switch(my_role.getRoleID()) {
      case Standards.ROLE_ID_PROGRAM_CHAIR:
        populatePanelWithProgramChairButtons(my_panel_buttons);
        my_panel_buttons.add(btn_cancel);
        break;
      case Standards.ROLE_ID_SUBPROGRAM_CHAIR:
        populatePanelWithSubprogramChairButtons(my_panel_buttons);
        my_panel_buttons.add(btn_cancel);
        break;
      case Standards.ROLE_ID_REVIEWER:
        populatePanelWithReviewerButtons(my_panel_buttons);
        my_panel_buttons.add(btn_cancel);
        break;
      default: //this should not happen ideally
        my_panel_buttons = super.createButtonsPanel();
    }
  }
  
  /**
   * Adds buttons with functionality for the Program Chair.
   * @param the_panel The panel to add buttons to.
   */
  private void populatePanelWithProgramChairButtons(final JPanel the_panel) {
    final ProgramChair pc = (ProgramChair) my_role;
    final JButton btn_accept = new JButton("Accept");
    btn_accept.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(final ActionEvent the_e) {
        if (pc.acceptManuscript(my_manuscript)) {
          JOptionPane.showMessageDialog(ManuscriptViewingPanel.this,
                                        "Manuscript successfully accepted!");
        } else {
          JOptionPane.showMessageDialog(ManuscriptViewingPanel.this,
                                        "Cannot accept this manuscript...");
        }
      }
    });
    final JButton btn_reject = new JButton("Reject");
    btn_reject.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(final ActionEvent the_e) {
        if (pc.rejectManuscript(my_manuscript)) {
          JOptionPane.showMessageDialog(ManuscriptViewingPanel.this,
                                        "Manuscript has been rejected!");
        } else {
          JOptionPane.showMessageDialog(ManuscriptViewingPanel.this,
                                        "Cannot reject this manuscript...");
        }
      }
    });
    
    the_panel.add(btn_accept);
    the_panel.add(btn_reject);
  }
  
  /**
   * Adds buttons with functionality for the Subprogram Chair.
   * @param the_panel The panel to add buttons to.
   */
  private void populatePanelWithSubprogramChairButtons(final JPanel the_panel) {
    final SubprogramChair spc = (SubprogramChair) my_role;
    final JButton btn_recommendation = new JButton("Make Recommendation...");
    btn_recommendation.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(final ActionEvent the_e) {
        if (new Date().after(my_conference.getReviewDeadline())) {
          JOptionPane.showMessageDialog(ManuscriptViewingPanel.this, ERROR_DEADLINE);
          return;
        }
        final DisplayPanel dsp = getDisplayStack();
        final RecommendationPanel rec = new RecommendationPanel(dsp, spc, my_manuscript);
        dsp.addToStack(rec);
      }
    });
    
    the_panel.add(btn_recommendation);
  }
  
  /**
   * Adds buttons with functionality for the Reviewer.
   * @param the_panel The panel to add buttons to.
   */
  private void populatePanelWithReviewerButtons(final JPanel the_panel) {
    final Reviewer reviewer = (Reviewer) my_role;
    final JButton btn_review = new JButton("Make Review...");
    btn_review.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(final ActionEvent the_e) {
        if (new Date().after(my_conference.getReviewDeadline())) {
          JOptionPane.showMessageDialog(ManuscriptViewingPanel.this, ERROR_DEADLINE);
          return;
        }
        final DisplayPanel dsp = getDisplayStack();
        final ReviewSubmissionPanel rsp =
            new ReviewSubmissionPanel(dsp, reviewer, my_manuscript);
        dsp.addToStack(rsp);
      }
    });
    
    the_panel.add(btn_review);
  }

}
